package cn.lopr.im.android.mvp.presenter;

import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.mvp.contact.IInfoContact;
import cn.lopr.im.android.mvp.model.InfoModel;
import cn.lopr.im.android.utils.ParameterUtil;
import cn.lopr.im.android.utils.UiUtils;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class InfoPresenterImpl implements IInfoContact.IInfoPresenter {
    private final InfoModel infoModel;
    private final Activity activity;
    private final IInfoContact.IInfoView infoView;

    public InfoPresenterImpl(Activity activity, IInfoContact.IInfoView infoView) {
        this.infoModel = new InfoModel();
        this.activity = activity;
        this.infoView = infoView;
    }

    @Override
    public void save() {
        if (!UiUtils.isNetworkAvailable()) {
            infoView.showNetworkError();
            return;
        }

        String name = infoView.getName();
        String birth = infoView.getBirth();
        String introduction = infoView.getIntroduction();
        String sex = infoView.getSex();

        if (ParameterUtil.checkParameter(name, birth, introduction, sex)) {
            infoView.showVerifyFailed(Constant.PARAM_ERROR_MESSAGE);
            return;
        }

        infoView.showLoading(this.activity);
        infoModel.update(name, birth, introduction, sex, new OnCallBackListener<String, String>() {

            @Override
            public void success(String data) {
                infoView.updateSuccess(data);
            }

            @Override
            public void failure(String errMsg) {
                infoView.disLoading();
                infoView.showVerifyFailed(errMsg);
            }
        });
    }

}
